package com.runda.service.impl;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.bbjob.mapper.VersionUpgradeMapper;
import com.bbjob.model.VersionUpgrade;
import com.bbjob.util.Tool;
import com.bbjob.util.service.impl.BaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.runda.service.IVersionUpgradeService;
import com.runda.task.AliyunOssUploadFromInputStream;

import tk.mybatis.mapper.entity.Example;

@Service
public class VersionUpgradeServiceImpl extends BaseService<VersionUpgrade> implements IVersionUpgradeService {

	@Value("${oss.endpoint}")
	private String endpoint;
	@Value("${oss.accessKeyId}")
	private String accessKeyId;
	@Value("${oss.accessKeySecret}")
	private String accessKeySecret;
	@Value("${oss.bucket}")
	private String bucket;

	@Resource
	private VersionUpgradeMapper versionmapper;

	@Override	
	public int upload(HttpServletRequest request, String username, MultipartFile multipartFile) {

		String versionNo = request.getParameter("versionNo");// 版本号
		String remark = request.getParameter("remark");// 版本升级描述
		Integer type = Integer.parseInt(request.getParameter("type"));// 1出题端2考试端
		try {
			InputStream inputStream = multipartFile.getInputStream();
			// 新增版本
			VersionUpgrade version = new VersionUpgrade();
			version.setVersionNo(versionNo);
			version.setCreaterId(username);
			version.setCreateTime(new Date());
			version.setRemark(remark);
			version.setType(type);
			version.setVersionStatus(0);
			int ok = versionmapper.insertSelective(version);
			if (ok > 0) {
				// 新增成功
				AliyunOssUploadFromInputStream thread = new AliyunOssUploadFromInputStream(endpoint, accessKeyId,
						accessKeySecret, Tool.getUUID() + ".exe", version.getId(), bucket, inputStream, versionmapper);
				thread.start();
				return 1;
			} else {
				return 0;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public VersionUpgrade checkUpgrade() {
		// TODO Auto-generated method stub
		Example ex = new Example(VersionUpgrade.class);
		Example.Criteria c = ex.createCriteria();
		c.andEqualTo("versionStatus", 1);
		c.andEqualTo("type", 1);
		ex.setOrderByClause(" create_time desc");
		List<VersionUpgrade> list = versionmapper.selectByExample(ex);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public PageInfo<VersionUpgrade> getlist(VersionUpgrade version, int page, int size) {
		PageHelper.startPage(page, size);
		Example example = new Example(VersionUpgrade.class);
		Example.Criteria criteria = example.createCriteria();
		if (StringUtil.isNotEmpty(version.getVersionNo())) {
			criteria.andLike("versionNo", "%" + version.getVersionNo() + "%");
		}
		if (StringUtil.isNotEmpty(version.getRemark())) {
			criteria.andLike("remark", "%" + version.getRemark() + "%");
		}
		if (version.getType()!= null) {
			criteria.andEqualTo("type", version.getType());
		}
		example.setOrderByClause(" create_time desc");
		List<VersionUpgrade> resultList = versionmapper.selectByExample(example);
		PageInfo<VersionUpgrade> result= new PageInfo<VersionUpgrade>(resultList);
		return result;
	}

	@Override
	public VersionUpgrade checkUpgradeForExam() {
		// TODO Auto-generated method stub
		Example ex = new Example(VersionUpgrade.class);
		Example.Criteria c = ex.createCriteria();
		c.andEqualTo("versionStatus", 1);
		c.andEqualTo("type", 2);
		ex.setOrderByClause(" create_time desc");
		List<VersionUpgrade> list = versionmapper.selectByExample(ex);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public int deleteUpgrade(Integer id) {
		// TODO Auto-generated method stub
		return versionmapper.deleteByPrimaryKey(id);
	}
}
